package com.eagle.concurrency.example.atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class RunOnece {
	private static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
	private static ExecutorService executorService = Executors.newCachedThreadPool();
	private static Semaphore semaphore = new Semaphore(200);

	public static void main(String[] args) throws Exception{

		for (int i = 0; i < 5000; i++) {
			executorService.execute(()->{
				try {
					semaphore.acquire();
					runOnece();
					semaphore.release();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		executorService.shutdown();

	}

	private static void runOnece() {
		boolean result = atomicBoolean.compareAndSet(false, true);
		if(result){
			log.info("Only run one time");
		}
	}
}
